package xyz.jcat.spring.security.example;

import cn.hutool.core.util.RandomUtil;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Component
public class FilterInvocationSecurityMetadataSourceImpl implements FilterInvocationSecurityMetadataSource {

    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Override
    public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
        HttpServletRequest request = ((FilterInvocation) object).getHttpRequest();
        //请求路径
        String matchRequestUri = "/"+request.getMethod()+request.getRequestURI();


        //TODO
//        Set<Map.Entry<String, Collection<ConfigAttribute>>> entries = reqUriRoleCache.entrySet();
//
//        for (Map.Entry<String, Collection<ConfigAttribute>> entry : entries) {
//            if(antPathMatcher.match(entry.getKey(), matchRequestUri)) {
//                return entry.getValue();
//            }
//        }
//        return null;

        List<ConfigAttribute> list = new ArrayList<>();
        if(RandomUtil.randomBoolean()) {
            ConfigAttribute configAttribute = new SecurityConfig("1");
            list.add(configAttribute);
        }else {
            ConfigAttribute configAttribute = new SecurityConfig("4");
            list.add(configAttribute);
        }
        return null;
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return true;
    }


}
